/**
 * Created by Administrator on 2016/3/2.
 */
//游戏主场景中bottom栏帮助弹框
gpNamespace.HelpLayer = cc.Layer.extend({
	index:null,
	yellowBg:null,
	feedback:null,
	problem:null,
	basicPlay:null,

	menuToggle: null,
	contentField: null,
	content1Field: null,
	ctor: function (index) {
		this._super();
		if(index){
			this.index = index;
        }
        this.init();
    },
    init: function () {
//        cc.spriteFrameCache.addSpriteFrames(gpNamespace.helpRes.Help_plist);
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkhelpRes.PdkHelp_plist);
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkhelpRes.PdkHelp2_plist);
        var size = cc.winSize;

        //背景图层
        var background = new cc.Sprite(gpNamespace.pdkhelp.HelpLayerBg);
        background.attr({
            x: 108 + background.width / 2,
            y: 30 + background.height / 2,
            anchorX: 0.5,
            anchorY: 0.5
        });
        this.addChild(background);

        //我要反馈页签
        var page1On = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerTopItemFeedBackSelected);
        var page1Off = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerTopItemFeedBack);
        var page1 = new cc.MenuItemToggle(page1Off, page1On, this.page1CallbackFunction, this);
        this.page1=page1;
        page1.setSelectedIndex(1);
        page1.setUserData(this);
        //常见问题
        var page2On = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerTopItemQuestionSelected);
        var page2Off = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerTopItemQueston);
        var page2 = new cc.MenuItemToggle(page2Off, page2On, this.page2CallbackFunction, this);
        this.page2=page2;
        page2.setUserData(this);
        //基本玩法
        var page3On = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerTopItemMethondSelected);
        var page3Off = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerTopItemMethond);
        var page3 = new cc.MenuItemToggle(page3Off, page3On, this.page3CallbackFunction, this);
        this.page3=page3;
        page3.setUserData(this);

        var TopListMenu = new cc.Menu(page1, page2, page3);
        TopListMenu.attr({
        	x: size.width/2,
        	y: 530+page1.height/2
        });
        TopListMenu.alignItemsHorizontallyWithPadding(200);
        this.addChild(TopListMenu,3);

        //页签背景
        var yellowBg = new cc.Sprite(gpNamespace.pdkhelp.HelpLayerTopItemSelected);
        yellowBg.attr({
        	x: 125 + yellowBg.width / 2+344*0,
        	y: 482 + yellowBg.height / 2,
        	anchorX: 0.5,
        	anchorY: 0.5
        });
        this.yellowBg=yellowBg;
        this.addChild(yellowBg,2);
        
        //内容主题背景
        var contentBg = new cc.Sprite(gpNamespace.pdkhelp.HelpLayerListBg);
        contentBg.attr({
        	x: 138,
        	y: 108,
        	anchorX: 0,
        	anchorY: 0
        });
        this.addChild(contentBg);

        //我要反馈
        var feedback = new tableViewPro.TableView(this, cc.size(1000, 340), 0);
        feedback.setPosition(140, 136);
        feedback.setAnchorPoint(0, 0);
        this.addChild(feedback);
        feedback.setBarOffset(0, 0); 

        feedback.clearData();
        feedback.addData({
        	"barInfo": {"width": 2, "height": 100},
        	"sameItem": [
        	             {
        	            	 "dataType": 1,
        	            	 "headInfo": {
        	            		 "headType": 0,
        	            		 "offset": 0,
        	            		 "imageName": "",
        	            		 "width": 0
        	            	 },
        	            	 "pageInfo": {"colCount": 1},
        	            	 "cellInfo": {
        	            		 "cellType": 0,
        	            		 "cellCount": 3,
        	            		 "imageName": "",
        	            		 "height": 150
        	            	 }
        	             }
        	             ]
        });
        feedback.showData();
        feedback.visible=true;
        feedback.setTouchEnabled(true);
        this.feedback=feedback;

        //cells上加表现
        var cellCount = feedback.currentItem.cellInfo.cellCount;
        var cellType = feedback.currentItem.cellInfo.cellType;
        var currentPage = feedback.currentPage;
        for (var a = 0; a < cellCount; a++) {
        	var cell;
        	switch (cellType) {
        	case 0://sprite
        		cell = currentPage.getChildByTag(a);
        		break;
        	case 1://menu
//        		cell = currentPage.getChildByName(xzgbNamespace.TVMENU).getChildByTag(a);
        		break;
        	}
        	this.setFeedbackCell(a, cell);
        }

        //常见问题
        var problem = new tableViewPro.TableView(this, cc.size(1000, 340), 0);
        problem.setPosition(140, 136);
        problem.setAnchorPoint(0, 0);
        this.addChild(problem);
        problem.setBarOffset(0, 0); 

        problem.clearData();
        problem.addData({
        	"barInfo": {"width": 2, "height": 50},
        	"sameItem": [
        	             {
        	            	 "dataType": 1,
        	            	 "headInfo": {
        	            		 "headType": 0,
        	            		 "offset": 0,
        	            		 "imageName": "",
        	            		 "width": 0
        	            	 },
        	            	 "pageInfo": {"colCount": 1},
        	            	 "cellInfo": {
        	            		 "cellType": 0,
        	            		 "cellCount": 1,
        	            		 "imageName": "#pdk_bz_017u.png",
        	            		 "height": 1884
        	            	 }
        	             }
        	             ]
        });
       problem.showData();
       problem.visible=false;
       problem.setTouchEnabled(false);
       this.problem=problem;

       //基本玩法
       var basicPlay = new tableViewPro.TableView(this, cc.size(1000, 340), 0);
       basicPlay.setPosition(140, 136);
       basicPlay.setAnchorPoint(0, 0);
       this.addChild(basicPlay);
       basicPlay.setBarOffset(0, 0); 

       basicPlay.clearData();
       basicPlay.addData({
    	   "barInfo": {"width": 2, "height": 50},
    	   "sameItem": [
    	                {
    	                	"dataType": 1,
    	                	"headInfo": {
    	                		"headType": 0,
    	                		"offset": 0,
    	                		"imageName": "",
    	                		"width": 0
    	                	},
    	                	"pageInfo": {"colCount": 1},
    	                	"cellInfo": {
    	                		"cellType": 0,
    	                		"cellCount": 1,
    	                		"imageName": gpNamespace.pdkhelp.HelpLayerMethond,
    	                		"height": 1190
    	                	}
    	                }
    	                ]
       });
       basicPlay.showData();
       basicPlay.visible=false;
       basicPlay.setTouchEnabled(false);
       this.basicPlay=basicPlay;    

       //设置显示页面
       if(this.index){
    	   switch (this.index) {
		case 2:
			this.page2CallbackFunction(page2);
			break;
		case 3:
			this.page3CallbackFunction(page3);
			break;
		default:
			this.page1CallbackFunction(page1);
			break;
		}
       }

       gpNamespace.TyNetManager.getInstance().setHelpLayer(this);
       
    },

    //设置反馈的回调函数
    setFeedbackCell: function (index, cell) {
    	switch (index) {
    	case 0:
    		var item1Bg = new cc.Sprite(gpNamespace.pdkhelp.HelpLayerChar);
    		item1Bg.attr({
    			x: 50,
    			y: 0,
    			anchorX: 0,
    			anchorY: 0
    		});
    		cell.addChild(item1Bg);

    		var menuItems = [];
    		for (var a = 0; a < 4; a++) {
    			var quan = new cc.Sprite(gpNamespace.pdkhelp.HelpLayerSelectedButtonNormal);
    			quan.attr({
    				x: 83 + 225 * a,
    				y: 5,
    				anchorX: 0.5,
    				anchorY: 0.5
    			});
    			cell.addChild(quan);

    			var itemOff = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerSelectedButtonNormal);
    			var itemOn = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerSelectedButtonSelected);
    			var itemToggle = new cc.MenuItemToggle(itemOff, itemOn, this.itemCallbackFunction, this);
    			if (a == 0)
    				itemToggle.setSelectedIndex(1);
    			itemToggle.setPosition(83 + 225 * a, 10);
    			itemToggle.setUserData([this, (a - 1 + 4) % 4]);
    			menuItems.push(itemToggle);
    		}

    		var item1 = menuItems.shift();
    		menuItems.push(item1);

    		var menuToggle = new cc.Menu(menuItems);
    		menuToggle.setPosition(0, 0);
    		cell.addChild(menuToggle);
    		this.menuToggle = menuToggle;
    		
    		break;

    	case 1:
    		//重要讯息填写
    		var item2Title = new cc.Sprite(gpNamespace.pdkhelp.HelpLayerImportantInforTv);
    		item2Title.attr({
    			x: 145,
    			y: 90
    		});
    		cell.addChild(item2Title);

    		//文本框背景
    		var labelBackground1 = new cc.Sprite(gpNamespace.pdkhelp.HelpLayerTelePhoneNumEt);
    		labelBackground1.attr({
    			x: 65,
    			y: 0,
    			anchorX: 0,
    			anchorY: 0
    		});
    		cell.addChild(labelBackground1);

    		//手机号输入框
    		var contentFieldSize = cc.size(640, 50);
    		var contentFieldScale9Sprite = new cc.Scale9Sprite(gpNamespace.pdkhelp.HelpLayerTelePhoneNumEt);//，貌似没软用
    		var contentField = new cc.EditBox(contentFieldSize, contentFieldScale9Sprite);
    		contentField.setPlaceHolder("请输入您的手机号码");
    		contentField.setPlaceholderFontName("Helvetica-Bold");
    		contentField.setMaxLength(11);
    		contentField.setPlaceholderFontColor(cc.color(255, 255, 255, 255));
    		contentField.setFontColor(cc.color(0, 0, 0, 255));
    		contentField.setPlaceholderFontSize(22);
    		contentField.setFontSize(22);
    		contentField.setInputMode(cc.EDITBOX_INPUT_MODE_PHONENUMBER);
    		contentField.setInputFlag(cc.EDITBOX_INPUT_FLAG_SENSITIVE);
    		contentField.setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);
    		contentField.setDelegate(this);
    		contentField.attr({
    			x: 5,
    			y: 3,
    			anchorX: 0,
    			anchorY: 0
    		});
    		labelBackground1.addChild(contentField);
    		this.contentField = contentField;
    		break;

    	case 2:
    		//反馈描述
    		var discribeTitle = new cc.Sprite(gpNamespace.pdkhelp.HelplayerFeedbackQuestionTv);
    		discribeTitle.attr({
    			x: 157,
    			y: 100
    		});
    		cell.addChild(discribeTitle);

    		//文本框背景
    		var labelBackground2 = new cc.Sprite(gpNamespace.pdkhelp.HelpLayerTelePhoneNumEt);
    		labelBackground2.attr({
    			x: 65,
    			y: 0,
    			anchorX: 0,
    			anchorY: 0
    		});
    		cell.addChild(labelBackground2);

    		//内容输入框
    		var content1FieldSize = cc.size(640, 50);
    		var content1FieldScale9Sprite = new cc.Scale9Sprite(gpNamespace.pdkhelp.HelpLayerFeedbackQuestionEt);
    		var content1Field = new cc.EditBox(content1FieldSize, content1FieldScale9Sprite);
    		content1Field.setPlaceHolder("请输入您的反馈内容");
    		content1Field.setPlaceholderFontName("Helvetica-Bold");
    		content1Field.setMaxLength(11);
    		content1Field.setPlaceholderFontColor(cc.color(255,255,255, 255));
    		content1Field.setFontColor(cc.color(0, 0, 0, 255));
    		content1Field.setPlaceholderFontSize(22);
    		content1Field.setFontSize(22);
    		content1Field.setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE);
    		content1Field.setInputFlag(cc.EDITBOX_INPUT_FLAG_SENSITIVE);
    		content1Field.setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);
    		content1Field.setDelegate(this);
    		content1Field.attr({
    			x: 5,
    			y: 3,
    			anchorX: 0,
    			anchorY: 0
    		});
    		labelBackground2.addChild(content1Field);
    		this.content1Field = content1Field;

    		//发送
    		var send = new cc.MenuItemImage(gpNamespace.pdkhelp.HelpLayerSendBn, gpNamespace.pdkhelp.HelpLayerSendBn, this.sendCallbackFunction, this);
    		send.attr({
    			x: 130,
    			y: 30
    		});
    		send.setUserData(this.menuToggle);
    		var menu = new cc.Menu(send);
    		menu.attr({
    			x: 720,
    			y: 10
    		});
    		cell.addChild(menu);
//    		menu.setMyParent(this.feedback);
    		/*this.scheduleOnce(function () {
//    			menu.setTouchArea(this.tv1.clippingNode.getContentSize(), this.tv1.clippingNode.getParent().convertToWorldSpace(this.tv1.clippingNode.getPosition()));
    		}, 0.5);*/
    		break;
    	}
    },

    itemCallbackFunction: function (sender) {
    	cc.log("反馈类别");

//    	tyNamespace.setMenuEnable(sender);

    	gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

    	var self = sender.getUserData()[0];
    	var index = sender.getUserData()[1];

    	var menuItems = self.menuToggle.getChildren();
    	for (var a = 0; a < menuItems.length; a++) {
    		menuItems[a].setSelectedIndex(0);
    		if (index == a)
    			menuItems[a].setSelectedIndex(1);
    	}
    },

    sendCallbackFunction: function (sender) {
    	cc.log("发送反馈");

    	tyNamespace.setMenuEnable(sender);

    	gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

    	var menuToggleItems = sender.getUserData().getChildren();

    	var feedBackType;
    	for (var a = 0; a < menuToggleItems.length; a++) {
    		if (menuToggleItems[a].getSelectedIndex() == 1) {
    			feedBackType = a;
    		}
    	}
    	feedBackType = (feedBackType + 2 + 4) % 4;
//    	gpNamespace.addWaitLayer();
//  	console.log("###############"+feedBackType+"####"+this.contentField.getString()+"####"+this.content1Field.getString());
    	if(/^1\d{10}$/.test(this.contentField.getString())){
//    		if(this.contentField.getString().length == 11 && this.content1Field.getString().length > 5){
    		xinyou.netManager.feedback(feedBackType, this.contentField.getString(), this.content1Field.getString());
    	}else{
//  		console.log(cc.sys.isMobile+"反馈失败，手机号位数不够或反馈内容太少");
    		tyNamespace.tipsJumper(1,"号码错误！");
    	}
    },
    
    setFankuiInfo: function () {
    	//跳个弹窗
    	tyNamespace.tipsJumper(1,tyNamespace.TipsSectencesIndex.FeedBackSuc);
    },
    
    page1CallbackFunction:function(sender){
//    	tyNamespace.setMenuEnable(sender);
    	var self = sender.getUserData();
    	self.yellowBg.x=125 + self.yellowBg.width / 2+344*0;
    	self.page1.setSelectedIndex(1);
    	self.page2.setSelectedIndex(0);
    	self.page3.setSelectedIndex(0);
    	self.feedback.visible=true;
    	self.feedback.setTouchEnabled(true);
    	self.problem.visible=false;
    	self.problem.setTouchEnabled(false);
    	self.basicPlay.visible=false;
    	self.basicPlay.setTouchEnabled(false);
    },
    page2CallbackFunction:function(sender){
//    	tyNamespace.setMenuEnable(sender);
    	var self = sender.getUserData();
    	self.yellowBg.x=125 + self.yellowBg.width / 2+344*1;
    	self.page1.setSelectedIndex(0);
    	self.page2.setSelectedIndex(1);
    	self.page3.setSelectedIndex(0);
    	self.feedback.visible=false;
    	self.feedback.setTouchEnabled(false);
    	self.problem.visible=true;
    	self.problem.setTouchEnabled(true);
    	self.basicPlay.visible=false;
    	self.basicPlay.setTouchEnabled(false);
    },
    page3CallbackFunction:function(sender){
//  	tyNamespace.setMenuEnable(sender);
    	var self = sender.getUserData();
    	self.yellowBg.x=125 + self.yellowBg.width / 2+344*2;
    	self.page1.setSelectedIndex(0);
    	self.page2.setSelectedIndex(0);
    	self.page3.setSelectedIndex(1);
    	self.feedback.visible=false;
    	self.feedback.setTouchEnabled(false);
    	self.problem.visible=false;
    	self.problem.setTouchEnabled(false);
    	self.basicPlay.visible=true;
    	self.basicPlay.setTouchEnabled(true);
    }
});